import { TextureLoader } from 'three';
import { Fn, int, ivec2, textureLoad } from 'three/tsl';

/**
 * @module Bayer
 * @three_import import { bayer16 } from 'three/addons/tsl/math/Bayer.js';
 */

let bayer16Texture = null;

/**
 * This TSL function can be used to sample a Bayer16 texture which is a 16x16 texture with a Bayer Matrix pattern.
 * It can be used for dithering effects but also as an alternative to blue-noise. When used with Ray Marching
 * specifically in {@link VolumeNodeMaterial#offsetNode}, it reduces banding problem, thus being able to use
 * fewer steps without affecting the visuals as much.
 *
 * @tsl
 * @function
 * @param {Node<vec2>} uv - The uv to sample the bayer16 texture.
 * @return {Node<vec4>} The sampled bayer value.
 */
export const bayer16 = Fn( ( [ uv ] ) => {

	if ( bayer16Texture === null ) {

		const bayer16Base64 = '';

		bayer16Texture = new TextureLoader().load( bayer16Base64 );

	}

	return textureLoad( bayer16Texture, ivec2( uv ).mod( int( 16 ) ) );

} );
